package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsPlatMchntInfoMapper;
import cc.rengu.igas.cops.common.entity.CopsPlatMchntInfo;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 平台商户正式表(T_B_PLAT_MCHNT_INFO)操作实现
 *
 * @author keyi
 * @date 2020/3/17.
 */
public class CopsPlatMchntInfoMapperImpl implements CopsPlatMchntInfoMapper {

    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public CopsPlatMchntInfo getOpmsPlatMchntInfoByInstIdMchntNo(String instId, String mchntNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsGetPlatMchntInfoByInstIdMchntNo");

        CopsPlatMchntInfo opmsPlatMchntInfo = new CopsPlatMchntInfo();
        opmsPlatMchntInfo.setInstId(instId);
        opmsPlatMchntInfo.setPlatMchntNo(mchntNo);

        int recode = dbsUtil.dbsExecuteSql(sqlStr, opmsPlatMchntInfo, opmsPlatMchntInfo);
        if (recode != Database.DBS_SUCCESS) {
            rglog.info("查询平台商户信息表失败，instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }

        return opmsPlatMchntInfo;
    }

    @Override
    public CopsPlatMchntInfo getOpmsPlatMchntInfoByInstIdMchntNoWithDefaultInstId(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsGetPlatMchntInfoByInstIdMchntNo");

        CopsPlatMchntInfo opmsPlatMchntInfo = new CopsPlatMchntInfo();
        opmsPlatMchntInfo.setInstId(instId);
        opmsPlatMchntInfo.setPlatMchntNo(mchntNo);

        int recode = dbsUtil.dbsExecuteSql(sqlStr, opmsPlatMchntInfo, opmsPlatMchntInfo);
        if (recode != Database.DBS_SUCCESS) {
            rglog.info("查询平台商户信息表失败，instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }

        return opmsPlatMchntInfo;
    }
}
